package io.github.talelin.latticy.common.util;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ljj
 * @Date 2022/4/14 16:22
 */
@SuppressWarnings("all")
public class BeanUtils {
    /**
     * 类型转换方法
     *
     * @param source 原类
     * @param clazz  要生成的类型
     * @param <T>    泛型
     * @return
     */
    public static <T> T copyProperties(Object source, Class<T> clazz) {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(clazz, "Class must not be null");
        T target = null;
        try {
            target = clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        BeanUtil.copyProperties(source, target);
        return target;
    }


    /**
     * Object 对象转 List
     *
     * @param obj
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> castList(Object obj, Class<T> clazz) {
        List<T> result = new ArrayList<T>();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }
}
